# coding: utf-8

from flask_wtf import FlaskForm

from wtforms.validators import Optional

from app.forms import NotChoices, NonValidatingSelectField
from app.op.constant import UserType
from app.op.models import WxUser, User


class SearchGroundUserForm(FlaskForm):
    """
    create by ld_wu 2018-02-28
    """

    invite_user_id = NonValidatingSelectField(
        '邀请人', validators=[Optional()], coerce=int
    )

    def __init__(self, only_ground=False, *args, **kwargs):
        super(SearchGroundUserForm, self).__init__(*args, **kwargs)

        user_ids = WxUser.get_all_invite_user_ids(only_ground)
        users = User.query.filter(
            User.id.in_(user_ids),
            User.type == UserType.GROUND_MARKETER.value
        ) if user_ids else list()

        self.invite_user_id.choices = [(0, '--选择邀请人--')] + \
                                      [
                                          (user.id, user.nickname)
                                          for user in users
                                      ]
        self.invite_user_id.default = NotChoices[0][0]
